package com.pfzy.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;

import com.google.android.material.snackbar.Snackbar;
import com.pfzy.ui.R;
import com.pfzy.ui.databinding.ActivityToastBinding;
import com.pfzy.ui.utils.CommonUtil;

public class ToastActivity extends AppCompatActivity implements View.OnClickListener {
    private ActivityToastBinding mBind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBind = ActivityToastBinding.inflate(getLayoutInflater());
        setContentView(mBind.getRoot());
        initView();
    }

    private void initView() {
        TextView title = mBind.toolbar.findViewById(R.id.layout_toolbar_title);
        title.setText("吐司");
        mBind.toolbar.findViewById(R.id.layout_toolbar_back).setOnClickListener(this);
        mBind.toast1.setOnClickListener(this);
        mBind.toast2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_toolbar_back:
                finish();
                break;
            case R.id.toast1:
//                Snackbar.make(v, "底部吐司", Snackbar.LENGTH_LONG).setAction("Action", null).show();
                showToast2("用户不存在 请重新输入", R.mipmap.common_warn);
                break;
            case R.id.toast2:
                showToast(this, "用户不存在 请重新输入", 5000);
                break;
            default:
                break;
        }
    }

    public static void showToast(Context context, String toastText, int duration) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.toast_layout, null);
//        TextView textView = view.findViewById(R.id.text);
//        textView.setText(toastText);
        Toast toast = new Toast(context);
        toast.setDuration(duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        view.setBackground(ResourcesCompat.getDrawable(context.getResources(), R.drawable.rectangle_b3000000_10dp, null));
        toast.setView(view);
        toast.show();
    }

    public void showToast2(String text,int id) {
        LayoutInflater inflater = getLayoutInflater();
        View customToast = inflater.inflate(R.layout.toast_layout, null);
        ImageView image = customToast.findViewById(R.id.toast_layout_image);
        TextView title = customToast.findViewById(R.id.toast_layout_title);
        title.setText(text);
        image.setImageDrawable(getResources().getDrawable(id));
        Toast toast = new Toast(CommonUtil.getContext());
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(customToast);
        toast.show();
    }
}
